﻿#pragma once
#include<iostream>
using namespace std;
//红黑树：二叉搜索树+4条红黑树规则
// 1.红黑树所有节点有且只有两种颜色：红、黑
// 2.红黑树的根必须为黑色
// 3.红色节点的孩子必须为黑色
// 4.对于任意一个节点，从这个节点开始到NULL节点的黑色节点数量都是一样的


// 枚举值表⽰颜⾊ 
enum Colour
{
	RED,
	BLACK,
};

// 这里我们默认按key/value结构实现 
template<class K, class V>
struct RBTreeNode
{
	// 这⾥更新控制平衡也要加⼊parent指针 
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;

	RBTreeNode(const pair<K, V>& kv)//const?
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		,_col(RED)
	{}
};

template<class K, class V>
class RBTree
{
	//typedef RBTreeNode<K, V> Node;
	using Node = RBTreeNode<K, V>;
public:

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}


	bool Insert(const pair<K, V>& kv)
	{
		Node* cur = _root;
		Node* parent = cur;

		if (!cur)//如果是空树
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		while(cur)
		{
			if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = parent->_left;
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = parent->_right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		if (parent->_kv.first > kv.first)
			parent->_left = cur;
		else
			parent->_right = cur;

		//连接至上一层
		cur->_parent = parent;


		//判断是否满足条件
		while (parent&&parent->_col == RED)
		{
			Node* pparent = parent->_parent;

			//先讨论parent在左边的情况
			if (pparent->_left == parent)
			{
				Node* uncle = pparent->_right;
				//uncle存在且为红 -> 变色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					pparent->_col = RED;

					cur = pparent;
					parent = cur->_parent;
				}
				else
				{
					//单旋+变色
					if (parent->_left == cur)
					{
						RotateR(pparent);
						pparent->_col = RED;
						parent->_col = BLACK;
						break;
					}
					//双旋+变色
					else
					{
						RotateL(parent);
						RotateR(pparent);
						pparent->_col = RED;
						cur->_col = BLACK;
						break;
					}
				}
			}
			else//再讨论parent在右边的情况
			{
				Node* uncle = pparent->_left;
				//uncle存在且为红 -> 变色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					pparent->_col = RED;

					cur = pparent;
					parent = cur->_parent;
				}
				else
				{
					//单旋+变色
					if (parent->_right == cur)
					{
						RotateL(pparent);
						parent->_col = BLACK;
						pparent->_col = RED;
						break;
					}
					else//双旋+变色
					{
						RotateR(parent);
						RotateL(pparent);
						cur->_col = BLACK;
						pparent->_col = RED;
						break;
					}
				}
			}
			_root->_col = BLACK;
		}
		return true;
	}

	//右单旋
	void RotateR(Node* parent)
	{
		Node* pparent = parent->_parent;
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		if(subLR)
			subLR->_parent = parent;
		parent->_left = subLR;
		
		subL->_right = parent;
		parent->_parent = subL;

		if (!pparent)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				subL->_parent = pparent;
				pparent->_left = subL;
			}
			else
			{
				subL->_parent = pparent;
				pparent->_right = subL;
			}
		}
	}

	//左单旋
	void RotateL(Node* parent)
	{
		Node* pparent = parent->_parent;
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		subR->_left = parent;
		parent->_parent = subR;

		if(!pparent)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = subR;
				subR->_parent = pparent;
			}
			else
			{
				pparent->_right = subR;
				subR->_parent = pparent;
			}
		}
	}

	//检查红黑树是否满足要求
	bool Check(Node* root, int blacknum,int refNum)
	{
		if (!root)
		{
			if (blacknum != refNum)
			{
				cout << "存在黑色节点不一致的路径";
				return false;
			}
			return true;
		}

		int num = blacknum;
		//检查根
		if (root == _root && _root->_col != BLACK)
			return false;
		
		//前序遍历验证：黑色节点数以及红色节点的孩子
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "出现连续红色节点";
			return false;
		}
		if (root->_col == BLACK)
		{
			num++;
		}

		return Check(root->_left, num,refNum) && Check(root->_right, num, refNum);
	}

	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
			return false;

		// 参考值 
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}

	void InOrder()
	{
		_InOrder(_root);
	}

private:
	void _InOrder(Node* root)
	{
		if (!root)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << " " << root->_kv.second << endl;
		_InOrder(root->_right);
	}

	Node* GetRoot()
	{
		return _root;
	}

	Node* _root = nullptr;
};